﻿using System;
using System.Collections.Generic;
using Game.Utilities.AwardUser;
using Game.Utilities.Chat;
using Game.Utilities.Log;


namespace Game.Utilities.Controller
{
    public class GameLogHandler
    {
        private static readonly Lazy<GameLogHandler> _instance = new Lazy<GameLogHandler>(() => new GameLogHandler());

        private   List<Award> _awards  ;
        private   List<ChatMessage> _chatMessages;

        public List<ChatMessage> ChatMessages {
            get { return _chatMessages; }
        }

        public List<Award> Awards
        {
            get { return _awards; }
        }


        public static GameLogHandler Instance
        {
            get { return _instance.Value; }
        }

        public GameLogHandler()
        {
            _awards = new List<Award>();
            _chatMessages =new List<ChatMessage>();
        }

        public ChatMessage TryAddMessage(string accounName, string message)
        {
            try
            {
                if (_chatMessages != null)
                {
                    if (_chatMessages.Count >= 100)
                    {
                        _chatMessages.RemoveAt(0);
                    }
                    var name = accounName;
                    if (name.Length > 3)
                        name = name.Substring(0, name.Length - 3) + "***";
                    var obj = new ChatMessage() { AccountName = name, Message = ChatFilter.RemoveBadWords(message)};
                    _chatMessages.Add(obj);
                    return obj;
                }
            }
            catch (Exception ex)
            {
                NLogManager.LogMessage("TryAddAward" + ex.ToString());
            }
            return null;
        }

        public Award TryAddAward(string accounName,int money,TypeEat  type)
        {
            try
            {

                if (_awards != null)
                {
                    if (_awards.Count >= 40)
                    {
                        _awards.RemoveAt(0);
                    }
                    var name = accounName;
                    if (name.Length > 3)
                        name = name.Substring(0, name.Length - 3) + "***";
                    var obj = new Award() {AcountName = name, Money = money, Type = type};
                    _awards.Add(obj);
                    return obj;
                }
            }
            catch (Exception ex)
            {
                NLogManager.LogMessage("TryAddAward" +ex.ToString());
               
            }
            return null;
        }

        
    }
}
